package com.sakura.mall.product.skuImages.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.skuImages.domain.entity.SkuImagesEntity;
import com.sakura.mall.product.skuImages.domain.request.AddSkuImagesRequest;
import com.sakura.mall.product.skuImages.domain.request.UpdateSkuImagesRequest;
import com.sakura.mall.product.skuImages.domain.request.SkuImagesPageRequest;
import com.sakura.mall.product.skuImages.domain.vo.SkuImagesVo;
import com.sakura.mall.product.skuImages.mapper.SkuImagesMapper;
import com.sakura.mall.product.skuImages.service.SkuImagesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * sku图片 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SkuImagesServiceImpl extends ServiceImpl<SkuImagesMapper, SkuImagesEntity> implements SkuImagesService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSkuImagesRequest request) {
        // todo 其他校验逻辑
        SkuImagesEntity skuImages = request.convert(SkuImagesEntity.class);
        this.save(skuImages);
    }

    @Override
    public Page<SkuImagesVo> queryPage(SkuImagesPageRequest request) {
        IPage<SkuImagesEntity> qp = request.toPage();
        LambdaQueryWrapper<SkuImagesEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SkuImagesVo> ret = qp.convert(entity -> entity.convert(SkuImagesVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSkuImagesRequest request) {
        SkuImagesEntity entity = request.convert(SkuImagesEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

